package com.yunti.springboot.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunti.springboot.bo.UserBO;
import com.yunti.springboot.config.component.AbstractCurrentRequestComponent;
import com.yunti.springboot.mapper.ClassMapper;
import com.yunti.springboot.po.Class;
import com.yunti.springboot.service.ClassService;
import com.yunti.springboot.util.GradeYearUtil;
import com.yunti.springboot.vo.Result;
import com.yunti.springboot.vo.req.ClassReq;
import com.yunti.springboot.vo.res.YearAndClassRes;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements ClassService {

    @Resource
    private AbstractCurrentRequestComponent abstractCurrentRequestComponent;

    @Resource
    ClassMapper mapper;

    @Override
    public Result getAllClass(ClassReq req) {
        if (req.getCurrent() == null) {
            req.setCurrent(0);
        }
        if (req.getSize() == null) {
            req.setSize(10);
        }
        Page page = new Page<>(req.getCurrent(), req.getSize());
        LambdaQueryWrapper<Class> classLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (req.getGrade() != null) {
            classLambdaQueryWrapper.eq(Class::getGrade,GradeYearUtil.getGradeYearByYear(req.getGrade()));
        }

        return Result.success(baseMapper.selectPage(page,classLambdaQueryWrapper));
    }

    @Override
    public Result getClassByYear(ClassReq req) {
        UserBO userBO = abstractCurrentRequestComponent.getCurrentStaffId();
        req.setSchoolId(userBO.getSchoolId());
        return Result.success(mapper.getClassByYear(req));
    }

    @Override
    public Result getAllYear() {
        return Result.success(mapper.getAllYear());
    }

    @Override
    public Result updateClass(Class req) {
        UserBO userBO = abstractCurrentRequestComponent.getCurrentStaffId();
        LambdaQueryWrapper<Class>lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Class::getSchoolId,userBO.getSchoolId());
        lambdaQueryWrapper.eq(Class::getGrade,req.getGrade());
        lambdaQueryWrapper.eq(Class::getClassName,req.getClassName());
        lambdaQueryWrapper.ne(Class::getId,req.getId());
        if (!mapper.selectList(lambdaQueryWrapper).isEmpty()){
            return Result.error("修改失败,该学校的【"+req.getClassName()+"】班级存在");
        }
        LocalDateTime now = LocalDateTime.now();
        req.setUpdateTime(now);
        int count = mapper.updateById(req);
        if (count == 0) {
            return Result.error("修改失败");
        }
        return Result.success("修改成功");
    }

    @Override
    public Result deleteClass(Class req) {
        int count = mapper.deleteById(req);
        if (count == 0) {
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }

    @Override
    public Result addClass(Class req) {
        UserBO userBO = abstractCurrentRequestComponent.getCurrentStaffId();
        LambdaQueryWrapper<Class>lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Class::getSchoolId,userBO.getSchoolId());
        lambdaQueryWrapper.eq(Class::getGrade,GradeYearUtil.getGradeYearByYear(req.getGrade()));
        lambdaQueryWrapper.eq(Class::getClassName,req.getClassName());
        if (!mapper.selectList(lambdaQueryWrapper).isEmpty()){
            return Result.error("添加失败,该学校的【"+req.getClassName()+"】班级存在");
        }

        LocalDateTime now = LocalDateTime.now();
        req.setCreateTime(now);
        req.setSchoolId(userBO.getSchoolId());
        req.setGrade(GradeYearUtil.getGradeYearByYear(req.getGrade()));
        int count = mapper.insert(req);
        if (count != 0) {
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @Override
    public Result findPage(ClassReq req) {
        if (req.getCurrent() == null) {
            req.setCurrent(0);
        }
        if (req.getSize() == null) {
            req.setSize(10);
        }
        Page page = new Page<>(req.getCurrent(), req.getSize());
        LambdaQueryWrapper<Class> classLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (req.getGrade() != null) {
            classLambdaQueryWrapper.eq(Class::getGrade,GradeYearUtil.getGradeYearByYear(req.getGrade()));
        }
        UserBO userBO = abstractCurrentRequestComponent.getCurrentStaffId();
        classLambdaQueryWrapper.eq(Class::getSchoolId,userBO.getSchoolId());
        classLambdaQueryWrapper.like(StrUtil.isNotBlank(req.getClassName()),Class::getClassName,req.getClassName());
        return Result.success(baseMapper.selectPage(page,classLambdaQueryWrapper));
    }

    @Override
    public Result getClassByGrade(ClassReq req) {
        UserBO userBO = abstractCurrentRequestComponent.getCurrentStaffId();
        req.setSchoolId(userBO.getSchoolId());
        req.setGrade(GradeYearUtil.getGradeYearByYear(req.getGrade()));
        return Result.success(mapper.getClassByGrade(req));
    }

}
